package com.orangelife.mobile.login.biz;

import android.os.Handler;

import com.curry.android.http.HttpUtil;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.constants.HouseURL;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小区的业务逻辑
 * Created by wgf on 2016/3/29.
 *
 * @author wgf
 * @since 3.6.0
 */
public class CommunityHttpBiz {

    public static void getCommunityList(String cityName, String desc, String longitude, String latitude, VolleyHttpUtil.ResponseCallBack callback, int pageNo, int pageSize) {
        VolleyHttpUtil.getInstance().cancel("TAG_COMM_LIST");
        HashMap<String, String> map = new HashMap<>();
        map.put("pageIndex", String.valueOf(pageNo));
        map.put("pageSize", String.valueOf(pageSize));
        map.put("desc", desc);
        map.put("cityName", cityName);
        map.put("longitude", longitude);
        map.put("latitude", latitude);
        VolleyHttpUtil.getInstance().getWithCallback(HouseURL.SERVICE_COMMUNITY, map, callback, "TAG_COMM_LIST");
    }

    public static List<Map<String, Object>> handleCommunityList(JSONObject response) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);
        }
        return list;
    }

    /** 获取城市列表 */
    public static void requestCityList(VolleyHttpUtil.ResponseCallBack callback) {
        VolleyHttpUtil.getInstance().getWithCallback(HouseURL.CITY, callback);
    }

    public static List<Map<String, Object>> handleCityList(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            List<Map<String, Object>> list = OrangeDataHandler.getListFromJsonWithPageEntity(response, new TypeToken<List<Map<String, Object>>>() {
            }.getType(), null, null);
            return list;
        }
        return null;
    }

    /**
     * 搜索城市（此功能已废弃）
     *
     * @param keyword 城市名（首字母，拼音，汉字）
     */
    public static void requestCityListByKeyword(String keyword, Handler handler, int what) {
        Map<String, Object> map = new HashMap<>();
        map.put("search", keyword);
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map", map);
        rootMap.put("url", Constant.PATH_SELECT_CITY + "?");
        rootMap.put("wat", what);
        new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

}
